/**
 * 
 */
package com.wegax.finprod.accountcard;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.vaadin.data.Item;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.Form;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.TextField;
import com.wegax.finprod.message.MessageCodes;
import com.wegax.finprod.message.Messages;
import com.wegax.finprod.wizardform.WizardForm;
import com.wegax.finprod.wizardform.WizardFormStep;

/**
 * @author wegax
 * 
 */
class SummaryWizardStep extends FormLayout implements WizardFormStep<PersonalAccountDebitCardMapper> {

	private static final Logger log = LoggerFactory.getLogger(SummaryWizardStep.class);

	private static final String NAME_FIELD = "name";

	private static final String SURNAME_FIELD = "surname";

	private static final String PESEL_FIELD = "pesel";

	private static final String DOCUMENT_IDENTITY_FIELD = "identityCard";

	private static final String EMAIL_FIELD = "email";

	private static final String COUNTRY1_FIELD = "country1";

	private static final String CITY1_FIELD = "city1";

	private static final String STREET1_FIELD = "street1";

	private static final String HOUSENR1_FIELD = "houseNr1";

	private static final String FLATNR1_FIELD = "flatNr1";

	private static final String POSTALCODE1_FIELD = "postalCode1";

	private static final String CORRESPONDANCE_AVAILABILITY_FIELD = "correspondanceAvailability";

	private static final String COUNTRY2_FIELD = "country2";

	private static final String CITY2_FIELD = "city2";

	private static final String STREET2_FIELD = "street2";

	private static final String HOUSENR2_FIELD = "houseNr2";

	private static final String FLATNR2_FIELD = "flatNr2";

	private static final String POSTALCODE2_FIELD = "postalCode2";

	private static final String ACCOUNTTYPE_FIELD = "accountType";

	private static final String ACCOUNTCURRENCY_FIELD = "accountCurrency";

	private static final String PAYCARDAVAILABILITY_FIELD = "paycardAvailability";

	private static final String PAYCARDKIND_FIELD = "paycardKind";

	private static final String EMBOSS_FIELD = "emboss";

	private WizardForm<PersonalAccountDebitCardMapper> wizardForm;

	private TextField nameTf;

	private TextField surnameTf;

	private TextField peselTf;

	private TextField identityCardTf;

	private TextField email;

	private ComboBox country1;

	private TextField city1;

	private TextField street1;

	private TextField houseNr1;

	private TextField flatNr1;

	private TextField postalCode1;

	private TextField correspondendAddressAvailability;

	private ComboBox country2;

	private TextField city2;

	private TextField street2;

	private TextField houseNr2;

	private TextField flatNr2;

	private TextField postalCode2;

	private ComboBox accountTypeField;

	private ComboBox accountCurrencyField;

	private TextField paycardAvailabilityField;

	private ComboBox paycardKindField;

	private TextField embossField;

	private Form form;

	public SummaryWizardStep() {
		initComponents();
	}

	private void initPersonalInformationComponents() {
		nameTf = new TextField(Messages.getString(MessageCodes.NAME_FIELD_CAPTION));
		form.addField(NAME_FIELD, nameTf);

		surnameTf = new TextField(Messages.getString(MessageCodes.SURNAME_FIELD_CAPTION));
		form.addField(SURNAME_FIELD, surnameTf);

		peselTf = new TextField(Messages.getString(MessageCodes.PESEL_FIELD_CAPTION));
		form.addField(PESEL_FIELD, peselTf);

		identityCardTf = new TextField(Messages.getString(MessageCodes.IDENTITY_CARD_FIELD_CAPTION));
		form.addField(DOCUMENT_IDENTITY_FIELD, identityCardTf);
	}

	private void initComponentsReadOnlyMode(boolean readOnlyMode) {
		nameTf.setReadOnly(readOnlyMode);
		surnameTf.setReadOnly(readOnlyMode);
		peselTf.setReadOnly(readOnlyMode);
		identityCardTf.setReadOnly(readOnlyMode);

		email.setReadOnly(readOnlyMode);
		country1.setReadOnly(readOnlyMode);
		city1.setReadOnly(readOnlyMode);
		street1.setReadOnly(readOnlyMode);
		houseNr1.setReadOnly(readOnlyMode);
		flatNr1.setReadOnly(readOnlyMode);
		postalCode1.setReadOnly(readOnlyMode);
		correspondendAddressAvailability.setReadOnly(readOnlyMode);
		country2.setReadOnly(readOnlyMode);
		city2.setReadOnly(readOnlyMode);
		street2.setReadOnly(readOnlyMode);
		houseNr2.setReadOnly(readOnlyMode);
		flatNr2.setReadOnly(readOnlyMode);
		postalCode2.setReadOnly(readOnlyMode);

		accountTypeField.setReadOnly(readOnlyMode);
		accountCurrencyField.setReadOnly(readOnlyMode);
		paycardAvailabilityField.setReadOnly(readOnlyMode);
		paycardKindField.setReadOnly(readOnlyMode);
		embossField.setReadOnly(readOnlyMode);
	}

	private void initAddressInformationComponentsWithDefaultValues(PersonalAccountDebitCardMapper mapper) {
		email.setValue(mapper.getEmail());
		country1.select(mapper.getCountry1());
		city1.setValue(mapper.getCity1());
		street1.setValue(mapper.getStreet1());
		houseNr1.setValue(mapper.getHouseNr1());
		flatNr1.setValue(mapper.getFlatNr1());
		postalCode1.setValue(mapper.getPostalCode1());
		if (mapper.isCorespAddressAvailable()) {
			correspondendAddressAvailability
					.setValue(Messages.getString(MessageCodes.ADDRESS_CORRESPONDENCE_AVAILABLE));
			country2.setValue(mapper.getCountry2());
			city2.setValue(mapper.getCity2());
			street2.setValue(mapper.getStreet2());
			houseNr2.setValue(mapper.getHouseNr2());
			flatNr2.setValue(mapper.getFlatNr2());
			postalCode2.setValue(mapper.getPostalCode2());
		} else {
			correspondendAddressAvailability.setValue(Messages
					.getString(MessageCodes.ADDRESS_CORRESPONDENCE_NOT_AVAILABLE));
			country2.select(country2.getNullSelectionItemId());
			city2.setValue("");
			street2.setValue("");
			houseNr2.setValue("");
			flatNr2.setValue("");
			postalCode2.setValue("");
		}
		country2.setVisible(mapper.isCorespAddressAvailable());
		city2.setVisible(mapper.isCorespAddressAvailable());
		street2.setVisible(mapper.isCorespAddressAvailable());
		houseNr2.setVisible(mapper.isCorespAddressAvailable());
		flatNr2.setVisible(mapper.isCorespAddressAvailable());
		postalCode2.setVisible(mapper.isCorespAddressAvailable());
	}

	private void initPersonalInformationComponentsWithDefaultValues(PersonalAccountDebitCardMapper mapper) {
		nameTf.setValue(mapper.getName());
		surnameTf.setValue(mapper.getSurname());
		peselTf.setValue(mapper.getPesel());
		identityCardTf.setValue(mapper.getIdentityCard());
	}

	private void initProductComponentsWithDefaultValues(PersonalAccountDebitCardMapper mapper) {
		accountTypeField.select(mapper.getAccountType());
		accountCurrencyField.select(mapper.getCurrencyType());

		if (mapper.isPaycardKindAvailability()) {
			paycardAvailabilityField.setValue(Messages.getString(MessageCodes.PAYCARD_AVAILABILITY_AVAILABLE));
			paycardKindField.select(mapper.getPaycardType());
			paycardKindField.setVisible(true);
			embossField.setValue(mapper.getEmboss());
			embossField.setVisible(true);
		} else {
			paycardAvailabilityField.setValue(Messages.getString(MessageCodes.PAYCARD_AVAILABILITY_NOT_AVAILABLE));
			paycardKindField.select(paycardKindField.getNullSelectionItemId());
			paycardKindField.setVisible(false);
			embossField.setValue("");
			embossField.setVisible(false);
		}
	}

	private void initProductComponents() {
		accountTypeField = new ComboBox(Messages.getString(MessageCodes.ACCOUNT_TYPE_FIELD_CAPTION));
		for (AccountType accType : AccountType.values()) {
			Item accTypeItem = accountTypeField.addItem(accType);
			accountTypeField.setItemCaption(accType, Messages.getString(MessageCodes.ACCOUNT_TYPE_PREFIX + accType));
		}
		form.addField(ACCOUNTTYPE_FIELD, accountTypeField);

		accountCurrencyField = new ComboBox(Messages.getString(MessageCodes.ACCOUNT_CURRENCY_FIELD_CAPTION));
		for (CurrencyType currencyType : CurrencyType.values()) {
			Item currencyTypeItem = accountCurrencyField.addItem(currencyType);
			accountCurrencyField.setItemCaption(currencyType,
					Messages.getString(MessageCodes.ACCOUNT_CURRENCY_PREFIX + currencyType));
		}
		form.addField(ACCOUNTCURRENCY_FIELD, accountCurrencyField);

		paycardAvailabilityField = new TextField(Messages.getString(MessageCodes.PAYCARD_AVAILABILITY_FIELD_CAPTION));
		form.addField(PAYCARDAVAILABILITY_FIELD, paycardAvailabilityField);

		paycardKindField = new ComboBox(Messages.getString(MessageCodes.PAYCARD_KIND_FIELD_CAPTION));
		for (PaycardType paycardType : PaycardType.values()) {
			Item currencyTypeItem = paycardKindField.addItem(paycardType);
			paycardKindField.setItemCaption(paycardType,
					Messages.getString(MessageCodes.PAYCARD_KIND_PREFIX + paycardType));
		}
		form.addField(PAYCARDKIND_FIELD, paycardKindField);

		embossField = new TextField(Messages.getString(MessageCodes.EMBOSS_FIELD_CAPTION));
		form.addField(EMBOSS_FIELD, embossField);
	}

	private void initAddressInformationComponents() {
		email = new TextField(Messages.getString(MessageCodes.EMAIL_FIELD_CAPTION));
		form.addField(EMAIL_FIELD, email);

		country1 = new ComboBox(Messages.getString(MessageCodes.COUNTRY_FIELD_CAPTION));
		for (CountryCode countryCode : CountryCode.values()) {
			country1.addItem(countryCode);
			country1.setItemCaption(countryCode, Messages.getString(MessageCodes.COUNTRY_CODE_CAPTION + countryCode));
		}
		form.addField(COUNTRY1_FIELD, country1);

		city1 = new TextField(Messages.getString(MessageCodes.CITY_FIELD_CAPTION));
		form.addField(CITY1_FIELD, city1);

		street1 = new TextField(Messages.getString(MessageCodes.STREET_FIELD_CAPTION));
		form.addField(STREET1_FIELD, street1);

		houseNr1 = new TextField(Messages.getString(MessageCodes.HOUSENR_FIELD_CAPTION));
		form.addField(HOUSENR1_FIELD, houseNr1);

		flatNr1 = new TextField(Messages.getString(MessageCodes.FLATNR_FIELD_CAPTION));
		form.addField(FLATNR1_FIELD, flatNr1);

		postalCode1 = new TextField(Messages.getString(MessageCodes.POSTALCODE_FIELD_CAPTION));
		form.addField(POSTALCODE1_FIELD, postalCode1);

		correspondendAddressAvailability = new TextField(
				Messages.getString(MessageCodes.ADDRESS_CORRESPONDENCE_FIELD_CAPTION));
		form.addField(CORRESPONDANCE_AVAILABILITY_FIELD, correspondendAddressAvailability);

		country2 = new ComboBox(Messages.getString(MessageCodes.COUNTRY_FIELD_CAPTION));
		for (CountryCode countryCode : CountryCode.values()) {
			country2.addItem(countryCode);
			country2.setItemCaption(countryCode, Messages.getString(MessageCodes.COUNTRY_CODE_CAPTION + countryCode));
		}
		form.addField(COUNTRY2_FIELD, country2);

		city2 = new TextField(Messages.getString(MessageCodes.CITY_FIELD_CAPTION));
		form.addField(CITY2_FIELD, city2);

		street2 = new TextField(Messages.getString(MessageCodes.STREET_FIELD_CAPTION));
		form.addField(STREET2_FIELD, street2);

		houseNr2 = new TextField(Messages.getString(MessageCodes.HOUSENR_FIELD_CAPTION));
		form.addField(HOUSENR2_FIELD, houseNr2);

		flatNr2 = new TextField(Messages.getString(MessageCodes.FLATNR_FIELD_CAPTION));
		form.addField(FLATNR2_FIELD, flatNr2);

		postalCode2 = new TextField(Messages.getString(MessageCodes.POSTALCODE_FIELD_CAPTION));
		form.addField(POSTALCODE2_FIELD, postalCode2);
	}

	private void initComponents() {
		form = new Form();
		addComponent(form);

		initPersonalInformationComponents();
		initAddressInformationComponents();
		initProductComponents();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.WizardStep#getCaption()
	 */
	@Override
	public String getCaption() {
		return Messages.getString(MessageCodes.SUMMARY_STEP_CAPTION);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.WizardStep#getContent()
	 */
	@Override
	public Component getContent() {
		return this;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.WizardStep#onAdvance()
	 */
	@Override
	public boolean onAdvance() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.vaadin.teemu.wizards.WizardStep#onBack()
	 */
	@Override
	public boolean onBack() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wegax.finprod.wizardform.WizardFormStep#getForm()
	 */
	@Override
	public Form getForm() {
		return form;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wegax.finprod.wizardform.WizardFormStep#map(java.lang.Object)
	 */
	@Override
	public void map(PersonalAccountDebitCardMapper bean) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.wegax.finprod.wizardform.WizardFormStep#initFormStep(com.wegax.finprod.wizardform.WizardForm)
	 */
	@Override
	public void initFormStep(WizardForm<PersonalAccountDebitCardMapper> wizardForm) {
		this.wizardForm = wizardForm;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wegax.finprod.wizardform.WizardFormStep#onShow()
	 */
	@Override
	public void onShow() {
		initComponentsReadOnlyMode(false);
		initPersonalInformationComponentsWithDefaultValues(wizardForm.getResultMapper());
		initAddressInformationComponentsWithDefaultValues(wizardForm.getResultMapper());
		initProductComponentsWithDefaultValues(wizardForm.getResultMapper());
		initComponentsReadOnlyMode(true);
	}
}
